package com.health.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.entity.Meal;
import com.health.entity.pojo.PageData;
import com.health.entity.pojo.dto.QueryMealDTO;
import com.health.entity.pojo.vo.MealVO;
import com.health.mapper.MealMapper;
import com.health.service.IMealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 套餐表 服务实现类
 * </p>
 *
 * @author sanchuan
 * @since 2023-04-18
 */
@Service
public class MealServiceImpl extends ServiceImpl<MealMapper, Meal> implements IMealService {

    @Autowired
    private MealMapper mealMapper;

    /**
     * 实现分页+条件查询+多表联查
     * @param pageNum
     * @param pageSize
     * @param queryMealDTO
     * @return
     */
    @Override
    public IPage<MealVO> listMealPage(Integer pageNum, Integer pageSize, QueryMealDTO queryMealDTO) {
        List<MealVO> MealVOS = mealMapper.listMealPage(queryMealDTO);
        List<MealVO> list = new ArrayList<>();
        int curIdx = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && curIdx + i < MealVOS.size(); i++) {
            list.add(MealVOS.get(curIdx + i));
        }
        IPage<MealVO> iPage = new Page<>(pageNum,pageSize);
        iPage.setRecords(list);
        iPage.setTotal(MealVOS.size());
        return iPage;
    }

    /**
     * 实现套餐上/下架
     * @param isShelves
     * @param id
     * @return
     */
    @Override
    public Boolean changeMedicalShelves(boolean isShelves, Integer id) {
        return mealMapper.changeMedicalShelves(isShelves,id);
    }

    /**
     * 批量上/下架套餐
     * @param isShelves
     * @param ids
     * @return
     */
    @Override
    public Boolean changeMedicalShelvesMulti(boolean isShelves, List<Integer> ids) {
        return mealMapper.changeMedicalShelvesMulti(isShelves,ids);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    public Boolean deleteMedicalShelvesMulti(List<Integer> ids) {
        return mealMapper.deleteMedicalShelvesMulti(ids);
    }
}
